package com.sogoodsoft.web.crm;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.Visit;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.crm.VisitService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.util.InfosysUtils;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

@Results({
	@Result(name="MYDATE",location="mydate.jsp"),
	@Result(name="MYVISIT",location="myvisit.jsp"),
	@Result(name="CHECKDATE",location="checkdate.jsp")
})
public class VisitAction extends CrudActionSupport < Visit > {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private VisitService visitService;
    
    @Autowired
    private SysClassService sysClassService;

    private Long id;

    private Visit entity;

    private Page < Visit > page = new Page < Visit >(WebConstants.PAGE_SIZE);
    
    /**拜访类型**/
    private Long visittypeid;
    
    /**客户ID**/
    private Long customeridid;
    
    private String actions;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setEntity(Visit entity) {
        this.entity = entity;
    }

    public Page < Visit > getPage() {
        return page;
    }

    public void setPage(Page < Visit > page) {
        this.page = page;
    }

    public Long getVisittypeid() {
        return visittypeid;
    }

    public void setVisittypeid(Long visittypeid) {
        this.visittypeid = visittypeid;
    }

    public Long getCustomeridid() {
        return customeridid;
    }

    public void setCustomeridid(Long customeridid) {
        this.customeridid = customeridid;
    }

    @Override
    public Visit getModel() {
        return entity;
    }

    @Override
    public String list() throws Exception {
        List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.DESC);
        }
        page = visitService.findPage(page, filters);
        return SUCCESS;
    }
    
    public String myVisit() throws Exception{
    	  List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
    	  User user = InfosysSecurityUtil.getCurrentUser();
     	 PropertyFilter prop = new PropertyFilter("EQL_visitorid",String.valueOf(user.getId()));
     	 filters.add(prop);
          if (!page.isOrderBySetted()) {
              page.setOrderBy("id");
              page.setOrder(Page.DESC);
          }
          page = visitService.findPage(page, filters);
    	return "MYVISIT";
    }
    
    public String myDate() throws Exception{
    	 List < PropertyFilter > filters = new ArrayList<PropertyFilter>();
		if ("formquery".equals(actions)) {
			filters = PropertyFilter.buildFromHttpRequest(Struts2Utils
					.getRequest());
		} else {
			//默认查询今天的预约
			PropertyFilter EQD = new PropertyFilter("GED_nextdate",InfosysUtils.YMD(new Date()));
			filters.add(EQD);
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, 1);
			PropertyFilter LED = new PropertyFilter("LED_nextdate",InfosysUtils.YMD(calendar.getTime()));
			filters.add(LED);
		}
    	
    	 User user = InfosysSecurityUtil.getCurrentUser();
    	 PropertyFilter prop = new PropertyFilter("EQL_visitorid",String.valueOf(user.getId()));
    	 filters.add(prop);
         if (!page.isOrderBySetted()) {
             page.setOrderBy("nextdate");
             page.setOrder(Page.DESC);
         }
         page = visitService.findPage(page, filters);
    	return "MYDATE";
    }
    
    public String checkDate() throws Exception{
    	prepareModel();
    	return "CHECKDATE";
    }
 

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    public String save() throws Exception {
        SysClass sc = sysClassService.get(visittypeid);
        entity.setVisittype(sc);
        visitService.save(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        return null;
    }

    @Override
    public String delete() throws Exception {
        entity = visitService.get(id);
        visitService.delete(entity);
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = visitService.get(id);
        } else {
            entity = new Visit();
        }
    }

	public String getActions() {
		return actions;
	}

	public void setActions(String actions) {
		this.actions = actions;
	}


    
    

}
